package com.polaris.live.widget.refresh

import android.content.Context
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.viewbinding.ViewBinding
import com.chad.library.adapter.base.viewholder.BaseViewHolder

/**
 * BaseBindingViewHolder
 *
 * @author Created by lucas on 2023/10/17 14:34
 */
abstract class BaseBindingViewHolder<VB : ViewBinding, T>(val mBinding: VB) : BaseViewHolder(mBinding.root) {
    /**
     * 绑定数据
     */
    abstract fun bind(item: T)
}

abstract class BaseAsyncViewHolder<VB : ViewBinding, T>(
    context: Context,
    private val viewGroup: ViewGroup = FrameLayout(context)
) : BaseViewHolder(viewGroup) {

    var mBinding: VB? = null
    var mDelayItem: T? = null

    fun bindView(binding: VB) {
        mBinding = binding
        viewGroup.addView(binding.root)

        mDelayItem?.also { bind(it) }
    }

    /**
     * 绑定数据
     */
    fun bind(item: T) {
        val binding = mBinding
        if (binding == null) {
            mDelayItem = item
        } else {
            bind(binding, item)
        }
    }

    abstract fun bind(binding: VB, item: T)

}